<?php
namespace app\index\controller;

use app\common\controller\HomeBase;
use app\common\model\Wenan as WenanModel;
use app\common\model\Images as ImagesModel;
use app\common\model\Audit as AuditModel;

class Download extends HomeBase
{
    protected $wacategory_model;
    protected $wenan_model;
    protected $images_model;
    protected $audit_model;

    protected function _initialize(){
        parent::_initialize();
        $this->wenan_model = new WenanModel();
        $this->images_model = new ImagesModel();
        $this->audit_model = new AuditModel();
    }

    public function download($type,$id){
        $where = [
            'w.id' => ['eq',$id],
            'w.examine' => ['eq',1],
            'w.status' => ['eq',1],
        ];
        $rec_wenan = [];
        $rec_img = [];
        if($type == 'wenan'){
            $res = $this->wenan_model->alias('w')->field('w.*,c.name')->where($where)->join('think_wacategory c','c.id = w.wid')->find();
            if(!$res){
                $this->redirect('/index/index/index');
            }
            $rec_wenan = $this->wenan_model->where('examine',1)->where('type',0)->order('readding desc')->limit(4)->select();
        }else if ($type == 'image') {
            $res = $this->images_model->alias('w')->field('w.*,c.name')->where($where)->join('think_imgcategory c','c.id = w.pid')->find();
            if(!$res){
                $this->redirect('/index/index/index');
            }
            $rec_img = $this->images_model->where('examine',1)->where('type',0)->order('readding desc')->limit(4)->select();
        } else if($type == 'shenhe'){
            $res = $this->audit_model->alias('w')->field('w.*,c.name')->where($where)->join('think_jumpcategory c','c.id = w.wid')->find();
            if(!$res){
                $this->redirect('/index/index/index');
            }
            $rec_wenan = $this->audit_model->where('examine',1)->where('type',0)->where('status',1)->order('readding desc')->limit(4)->select();
        } else {
            $this->redirect('/index/index/index');
        }
        $this->assign('rec_img',$rec_img);
        $this->assign('rec_wenan',$rec_wenan);
        $this->assign('res_info',$res);
        return $this->fetch('download',['type' => $type]);
    }

    public function getfile($id,$type){
        $where = [
            'id' => ['eq',$id],
            'examine' => ['eq',1]
        ];
        if($type == 'wenan'){
            $download = $this->wenan_model->where($where)->value('compress');
            if(!$download) $this->redirect('/index/index/index');
            $this->wenan_model->where('id',$id)->setInc('downloadding');
            $name = 'DSJ_'.md5(microtime(true));
            header("Content-Type:application/octet-stream;charset=utf-8");
            header('Content-Disposition: attachment; filename = '.$name.'.rar');
            header('X-Accel-Redirect:'.$download);
        }else if ($type == 'image') {
            $download = $this->images_model->where($where)->value('path');
            if(!$download) $this->redirect('/index/index/index');
            $this->images_model->where('id',$id)->setInc('downloadding');
            $last_dot = strripos($download,".");
            $suffix = substr($download,$last_dot);
            $name = 'DSJ_'.md5(microtime(true));
            header("Content-Type:application/octet-stream;charset=utf-8");
            header('Content-Disposition: attachment; filename = '.$name.$suffix);
            header('X-Accel-Redirect:'.$download);
        } else if($type == 'shenhe'){
            $download = $this->audit_model->where($where)->value('compress');
            if(!$download) $this->redirect('/index/index/index');
            $this->audit_model->where('id',$id)->setInc('downloadding');
            $name = 'DSJ_'.md5(microtime(true));
            header("Content-Type:application/octet-stream;charset=utf-8");
            header('Content-Disposition: attachment; filename = '.$name.'.rar');
            header('X-Accel-Redirect:'.$download);
        }

    }
}